package com.musemun.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.musemun.common.CommonResult;
import com.musemun.contants.ReturnType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SecurityException implements AccessDeniedHandler {
    private Integer code;
    private String msg;

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        ReturnType.returnJson(httpServletResponse);
        httpServletResponse.getWriter().write(JSON.toJSONString(new CommonResult<String>(code, msg)));
        httpServletResponse.flushBuffer();
    }
}
